package main

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	mr "mapreduce"
	"os"
	"regexp"
	"strconv"
	"strings"
	"unicode"
)
//func main(){
//	mr.Sequential()
//
//}
func generate_input(dir string) []string{
	filePath :=[]string{}
	//正则遍历,file descriptor
	dirs,err := ioutil.ReadDir(dir)
	if err!=nil {
		log.Fatalf("cannot read file")
	}
	PthSep := string(os.PathSeparator)
	for _,fd:=range dirs{
		fmt.Println(fd.Name())
		bool,_:=regexp.MatchString("pg",fd.Name())
		if bool{
			filePath = append(filePath, dir+PthSep+fd.Name())
		}
	}
	return filePath
}
func main(){
	mapf := func(filename string, contents string) []mr.KeyValue {
		// function to detect word separators.
		ff := func(r rune) bool { return !unicode.IsLetter(r) }
		// split contents into an array of words.
		words := strings.FieldsFunc(contents, ff)

		kva := []mr.KeyValue{}
		for _, w := range words {
			//简单作为单项加入数组，并不执行累加操作
			kv := mr.KeyValue{w, "1"}
			kva = append(kva, kv)
		}
		return kva
	}
	//reduce定义,(k2,list(v2))->list(v2)
	reducef:=func(key string, values []string) string {
		// return the number of occurrences of this word.
		return strconv.Itoa(len(values))
	}
	arg1 := generate_input("./src/main")
	fmt.Println(arg1)
	//mr.Worker(mapf,reducef)
	mr.Sequential("Hello",arg1[0:1],10,mapf,reducef)
	//mr.Wait()
}
func main1(){
	//array marshal and unmarshal
	kvs := []mr.KeyValue{}
	kvs = append(kvs,mr.KeyValue{"Alan","1"})
	kvs = append(kvs,mr.KeyValue{"FL","1"})
	fd,err:=os.Create("temp.txt")
	if err!=nil{
		panic("Open error")
	}
	enc:=json.NewEncoder(fd)
	for _,v := range(kvs){
		enc.Encode(v)
	}
	fd.Close();
	fd,err1:=os.Open("temp.txt")
	if err1!=nil{
		panic("Open error")
	}
	decoder:=json.NewDecoder(fd)
	kvs1:=[]mr.KeyValue{}
	//using for loop to read json file,stream io
	for{
		var kv mr.KeyValue
		if err:=decoder.Decode(&kv);err==io.EOF{
			break;
		}else if err!=nil{
			log.Fatal(err)
		}
		fmt.Printf("%s:%s\n",kv.Key,kv.Value)
		kvs1 = append(kvs1,kv)
	}
	fmt.Printf("Length is:%d\n",len(kvs1))
	for _,kv:=range kvs1{
		fmt.Printf("%s:%s\n",kv.Key,kv.Value)
	}
}
